package com.yq.day28;

/**
 * @author 3307235355@qq.com
 * @since 2022/06/28 14:59
 */
public class Demo02 {
    public static void main(String[] args) {

        //每一个窗口里边都有一个新的100
        Sell2 m1 = new Sell2();

        Thread t1 = new Thread(m1);
        Thread t2 = new Thread(m1);
        Thread t3 = new Thread(m1);

        t1.setName("a窗口");
        t2.setName("b窗口");
        t3.setName("c窗口");

        t1.start();
        t2.start();
        t3.start();

    }
}


class Sell2 implements Runnable {
    int tickets = 100;
    @Override
    public void run() {
        while (true) {
            /*
            判断，a抢到先执行，进来 t=100
            休眠的时候可能b,c抢到进来了，这个时候
            就会出现t还等于100的情况
             */
            if (tickets > 0) {
                //增加网络延迟 出现重复的票数，
                // 还有不存在的票数
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName() + "卖了" + (tickets--) + "票");
            }
        }
    }
}